package team.yidong.fenda.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import team.yidong.fenda.entity.Question;
import team.yidong.fenda.service.AnswerService;
import team.yidong.fenda.service.HospitalService;
import team.yidong.fenda.service.QuestionService;
import team.yidong.fenda.utils.ImageCheck;
import team.yidong.fenda.utils.QiNiuYunUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by xuanlong on 2017/10/31.
 */
@Controller
public class AnswerController {
    @Autowired
    AnswerService answerService;
    @Autowired
    ImageCheck imageCheck;
    @Autowired
    QiNiuYunUtil qiNiuYunUtil;
    @Autowired
    HospitalService hospitalService;
    @Autowired
    QuestionService questionService;

    @RequestMapping("/findAnswer")
    @ResponseBody
    public JSONArray findAnswer(@RequestParam Integer questionId) {

        return answerService.findAnswerByQuestionId(questionId);
    }

    @RequestMapping("/openAnswerImg")
    @ResponseBody
    public String openAnswerImg(@RequestParam Integer imgId) {
        return answerService.openAnswerImg(imgId);
    }

    @RequestMapping("/addAnswerContent")
    @ResponseBody
    public String addAnswerContent(HttpSession session, @RequestParam Integer answerContentQuestion, @RequestParam String answerContentText, HttpServletRequest request) throws ParseException {

        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("answerContentImg");
        String fileName = new String();
        List<String> stringList = new ArrayList<>();
        MultipartFile multipartFile = null;
        for (int i = 0; i < files.size(); ++i) {
            multipartFile = files.get(i);
            if (!multipartFile.isEmpty()) {
                try {
                    if (imageCheck.isImage(multipartFile.getBytes())) {
                        // 获取文件的后缀名
                        fileName = multipartFile.getOriginalFilename();
                        String suffixName = fileName.substring(fileName.lastIndexOf("."));
                        //uuid为文件命名
                        fileName = UUID.randomUUID() + suffixName;
                        qiNiuYunUtil.uploadFiles(multipartFile.getBytes(), fileName);
                        stringList.add("https://qny.liuxuanlong.xyz/" + fileName);
                    } else {
                        return "第" + i + "张图片不是图片文件";
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return "第" + i + "张图片上传失败";
                }
            }
        }
        String sysUserName = (String) session.getAttribute("loginSysUserName");

        Integer questionId = answerContentQuestion;

        answerService.addQuestionAnswer(sysUserName, questionId, answerContentText, stringList);

        return "添加成功";
    }

    @RequestMapping("/checkQuestionAnswerStatus")
    @ResponseBody
    public String checkQuestionAnswerStatus(@RequestParam Integer answerQuestionId) {
        JSONObject jsonObject = questionService.questionInfo(answerQuestionId);
        if ("过期无法回答".equals(jsonObject.getString("answerStatus"))) {
            return "error";
        } else {
            return "right";
        }

    }
}
